package test

import (
	"chat_gin/entity/models"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
)

var db *gorm.DB
var err error

func TestUser() {
	fmt.Println("hello IM更新表")

	dns := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		"root",
		"123456",
		"127.0.0.1",
		"3306",
		"hello",
	)
	fmt.Println(dns)
	db, err = gorm.Open(mysql.Open(dns), &gorm.Config{
		// gorm日志模式：silent
		//Logger: logger.Default.LogMode(logger.Silent), 不显示SQL
		Logger: logger.Default.LogMode(logger.Info), // 显示所有SQL
		// 外键约束
		DisableForeignKeyConstraintWhenMigrating: true,
		// 禁用默认事务（提高运行速度）
		SkipDefaultTransaction: true,
		NamingStrategy: schema.NamingStrategy{
			// 使用单数表名，启用该选项，此时，`User` 的表名应该是 `user`
			SingularTable: true,
		},
	})
	db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8")
	err = db.AutoMigrate(&models.Contract{}, &models.GroupBasic{}, &models.Message{}, &models.UserBasic{})
	if err != nil {
		fmt.Println("初始化user表失败", err)
		return
	}

	//user := models.UserBasic{
	//	Model:         gorm.Model{},
	//	Name:          "张三",
	//	Password:      "123456",
	//	Phone:         "13123456789",
	//	Email:         "123@qq.com",
	//	Identity:      "123",
	//	ClientIP:      "1.1.1.1",
	//	ClientPort:    "888",
	//	LoginTime:     time.Now(),
	//	HeartbeatTime: time.Now(),
	//	LogOutTime:    time.Now(),
	//	IsLogout:      false,
	//}
	//err2 := db.Model(&user).Create(&user).Error
	//if err2 != nil {
	//	fmt.Println("创建user用户是啊白", err2)
	//}

}
